package com.code;

/**
 * Created by lucky on 2018/9/29.
 */
public class MaxSequence {
    public static void main(String[] args) {
        int[] v = new int[]{-2,1,-3,4,-1,2,1,-5,4};
        int i = maxSubArrayOn(v);
        System.out.println(i);
    }

    public static int maxSubArrayOn2(int[] nums) {
        int q = -999;
        for (int i = 0 ; i < nums.length ; i++){
            int temp = nums[i] ;
            for (int j = i + 1 ; j <nums.length ; j++){
                temp += nums[j];
                if(temp > q){
                    q = temp;
                }
            }
        }
        return q;
    }

    public static int maxSubArrayOn(int[] nums) {
        int temp = nums[0];
        int sum =nums[0];
        for (int i = 1 ; i < nums.length ; i++){
            if(temp > 0){
                temp += nums[i];
            }else{
                temp = nums[i];
            }
            if(temp > sum){
                sum = temp;
            }
        }
        return sum;
    }
}
